

//
//  NHNetEngine.m
//  NeiHanDemo
//
//  Created by qianfeng on 16/2/19.
//  Copyright © 2016年 王卓越. All rights reserved.
//

#import "NHNetEngine.h"
#import "AFNetworking.h"


@implementation NHNetEngine

+ (instancetype)sharedInstance{
    //创建单例
    static NHNetEngine *s_instance = nil;
    //@synchronized ->为了防止多线程竞争
    //如果为空 -》创建；如果不为空，就不用再创建了
    @synchronized(self) {
        if (s_instance == nil) {
            s_instance = [[NHNetEngine alloc] init];
        }
    }
    return s_instance;
}

//request: ->等待请求的url
//success: ->数据请求成功后调用的block
//falied:  ->数据请求失败后调用的block
- (void)request:(NSString *)url success:(SuccessBlockType)successBlock falied:(FaliedBlockType)faliedBlock{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //failed: unacceptable content-type: text/html"
   manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject: @"text/html"];
    
   [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //把网络返回的数据，通过block回传回去
    if (successBlock) {
        successBlock(responseObject);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //如果失败，把错误信息，通过block回传回去
    if (faliedBlock) {
        faliedBlock(error);
    }
}];

}

@end
